<?php
class DBOperations
{
	public function __construct()
	{
		mysql_pconnect("localhost","root","")
		or die("Не мога да се свържа с MySQL: ".mysql_error());
		
		mysql_select_db("posmeise")
		or die("Не мога да открия базата данни: ". mysql_error());
		mysql_query("SET NAMES UTF8");
	}
	
	// Functions for UserForRegistration --------- START
	
	public static function does_user_name_exist($user_name)
	{
		$query = "SELECT user_name FROM user WHERE user_name=\"".$user_name."\" LIMIT 1";
		$query = mysql_query($query) or die (mysql_error());
		return mysql_num_rows($query);
	}
	
	public function does_email_exist($email)
	{
		$query = "SELECT email FROM user WHERE email=\"".$email."\" LIMIT 1";
		$query = mysql_query($query) or die (mysql_error());
		return mysql_num_rows($query);
	}
	
	public static function getUserPassword($user_name)
	{
		$query = "SELECT password FROM user WHERE user_name = \"$user_name\" LIMIT 1";
		$query = mysql_query($query);
		$password = mysql_result($query, 0, "password");
		return $password;
	}
	
	public function add_user($user)
	{
		$newPass = md5($user->getPassword());
		$inert_user_query = "INSERT INTO user (user_name, password, email, born_date, gender, activated) VALUES (\"".$user->getUser_name()."\", \"".$newPass."\", \"".$user->getEmail()."\", \"".$user->getBorn_date()."\", \"".$user->getGender()."\", 0)";
		mysql_query($inert_user_query) or die (mysql_error());
	}
	
	public function get_user_id_by_given_user_name_and_email($user)
	{
		$get_user_id_query = "SELECT user_id FROM user WHERE user_name = \"".$user->getUser_name()."\" AND email = \"".$user->getEmail()."\" LIMIT 1";
		$get_user_id = mysql_query($get_user_id_query) or die (mysql_error());
		$user_id = mysql_result($get_user_id, 0, "user_id");
		$user->setUser_id($user_id);
	}
	
	public function setActivationKey($user)
	{
		$key = substr (md5(uniqid(rand(),1)), 3, 16);
		$insert_key_query = "INSERT INTO activation_key VALUES (".$user->getUser_id().", \"$key\")";
		mysql_query($insert_key_query) or die (mysql_error());
		return $key;
	}
	
	public static function sendEmail($toEmail,$subject,$text)
	{
		
		$fromEmail = "admin@posmeise.com";
		$headers = 'From: ' . $fromEmail . "\r\n";
		$headers .= 'To: ' . $toEmail . "\r\n";    
		$headers .= 'Return-Path: ' . $fromEmail . "\r\n";
		$headers .= 'MIME-Version: 1.0' ."\r\n";
		$headers .= 'Content-Type: text/HTML; charset=UTF-8' . "\r\n";
		$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";

		mail ($toEmail, $subject, $text, $headers);
	}
	
	public static function getUserFromDB($user_name)
	{
		$query = "SELECT * FROM user WHERE user_name = \"" . $user_name . "\" LIMIT 1";
		$query = mysql_query($query)or die(mysql_error());
		$result = mysql_fetch_array($query);
		return $result;
	}
	
	// Functions for UserForRegistration --------- END
	
	// Functions for Content ------ START
	
	public static function insetrtContentIntoDB($type,$content,$user_id,$title)
	{
		$query = "INSERT INTO content(type,title,content,user_id) VALUES('$type','$title','$content','$user_id')";
		mysql_query($query) or die(mysql_error());
	}
	
	public static function insetrtPicIntoDB($type,$content,$user_id,$title)
	{
		
		$query = "INSERT INTO `posts` (`id`, `title`, `post`, `author`, `date`, `pic`) VALUES
		(NULL, '$title', '$post', '$author', '$date', '".$file_copy.$new_name_pic."')";
		mysql_query($query,$db_conection);
		$error = 'Новината е успешно добавена <a href="index.php">ТУК</a>.<br />';
	}
	
}
?>